<?php

require_once 'Album.php';
require_once 'AlbumForm.php';

class App_AlbumService
{
	protected $db;
	protected $album;
	protected $lastID;
	
	public function __construct()
	{
		$options = array(
			'host' => 'localhost',
			'username' => 'root',
			'password' => '',
			'dbname' => 'xpic'
		);
		
		$this->db = Zend_Db::factory('PDO_MYSQL', $options);
		Zend_Db_Table_Abstract::setDefaultAdapter($this->db);
		$this->album = new Album();
        $this->session = new Zend_Session_Namespace("default");
	}
	
	public function saveAlbum($name, $desc) 
	{
		$params = array(
			'UserID' => $this->session->userID,
			'Name' => $name,
			'Description' => $desc,
			'CreatedDate' => date('Y-m-d G:i:s'),
			'ModifiedDate' => date('Y-m-d G:i:s')
		);
		
		$this->lastID = $this->album->insert($params);
	}
	
	public function getAllAlbumsByUserID($userID)
	{
		$select = $this->album->select();
		$select->where('UserID = ?', $userID);
		return $this->album->fetchAll($select);
	}
	
	public function getDefaultAlbumID($userID)
	{
		$select = $this->album->select();
		$select->where('UserID = ?', $userID);
		$defAlbum = $this->album->fetchRow($select);
		if (!(is_null($defAlbum)))
		{
			$defAlbumID = $defAlbum['AlbumID'];
		}
		else
		{
			$defAlbumID = 0;
		}
		return $defAlbumID;	
	}
	
	public function getLastID()
	{
		return $this->lastID;
	}
}